package com.ruoyi.operation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.operation.domain.TFileGroup;
import com.ruoyi.operation.domain.bo.TFileGroupBo;
import com.ruoyi.operation.domain.vo.FileMonitorVo;
import com.ruoyi.operation.domain.vo.TFileGroupVo;
import com.ruoyi.operation.mapper.TFileGroupMapper;
import com.ruoyi.operation.service.ITFileGroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 文件组Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-22
 */
@RequiredArgsConstructor
@Service
public class TFileGroupServiceImpl implements ITFileGroupService {

    private final TFileGroupMapper baseMapper;

    /**
     * 查询文件组
     */
    @Override
    public TFileGroupVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询文件组列表
     */
    @Override
    public TableDataInfo<TFileGroupVo> queryPageList(TFileGroupBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TFileGroup> lqw = buildQueryWrapper(bo);
        Page<TFileGroupVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询文件组列表
     */
    @Override
    public List<TFileGroupVo> queryList(TFileGroupBo bo) {
        LambdaQueryWrapper<TFileGroup> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TFileGroup> buildQueryWrapper(TFileGroupBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TFileGroup> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), TFileGroup::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getHostip()), TFileGroup::getHostip, bo.getHostip());
        lqw.eq(StringUtils.isNotBlank(bo.getFullpath()), TFileGroup::getFullpath, bo.getFullpath());
        lqw.eq(StringUtils.isNotBlank(bo.getBusisystemid()), TFileGroup::getBusisystemid, bo.getBusisystemid());
        lqw.eq(StringUtils.isNotBlank(bo.getInorex()), TFileGroup::getInorex, bo.getInorex());
        lqw.eq(StringUtils.isNotBlank(bo.getPeriod()), TFileGroup::getPeriod, bo.getPeriod());
        lqw.eq(bo.getStarttime() != null, TFileGroup::getStarttime, bo.getStarttime());
        lqw.eq(StringUtils.isNotBlank(bo.getTestmode()), TFileGroup::getTestmode, bo.getTestmode());
        lqw.eq(StringUtils.isNotBlank(bo.getMemo()), TFileGroup::getMemo, bo.getMemo());
        lqw.eq(StringUtils.isNotBlank(bo.getFlag()), TFileGroup::getFlag, bo.getFlag());
        return lqw;
    }

    /**
     * 新增文件组
     */
    @Override
    public Boolean insertByBo(TFileGroupBo bo) {
        TFileGroup add = BeanUtil.toBean(bo, TFileGroup.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改文件组
     */
    @Override
    public Boolean updateByBo(TFileGroupBo bo) {
        TFileGroup update = BeanUtil.toBean(bo, TFileGroup.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TFileGroup entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除文件组
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<FileMonitorVo> selectAllMonitorList(String groupId) {
        return baseMapper.selectFileMonitorList(groupId);
    }
}
